package code.example.base.reflect.Type;

import code.example.tool.LogFactory;

import java.lang.reflect.*;

public class TestType {

    public void showType(Type type) {
        if (Class.class.isInstance(type)) {
            Class classz = (Class) type;
            LogFactory.getLog().info("class:" + classz.getCanonicalName());
        } else if (ParameterizedType.class.isInstance(type)) {
            ParameterizedType parameterizedType = (ParameterizedType) type;
            String typeName = parameterizedType.getTypeName();
            LogFactory.getLog().info("parameterizedType:" + typeName);
            Type[] types = parameterizedType.getActualTypeArguments();
            LogFactory.getLog().info("泛型：");
            for (Type acturlType : types) {
                showType(acturlType);
            }
            LogFactory.getLog().info("实际类型：");
            Type rawType = parameterizedType.getRawType();
            showType(rawType);
        } else if (TypeVariable.class.isInstance(type)) {
            TypeVariable typeVariable = (TypeVariable)type;
            String name = typeVariable.getName();
            LogFactory.getLog().info("TypeVariable:"+name);
        } else if(GenericArrayType.class.isInstance(type)){
            GenericArrayType genericArrayType = (GenericArrayType)type;
            Type companyType = genericArrayType.getGenericComponentType();
            LogFactory.getLog().info("GenericArrayType.componentType:"+companyType.getTypeName());
        } else if(WildcardType.class.isInstance(type)){
            WildcardType wildcardType = (WildcardType)type;
            Type[] lowerTypes =  wildcardType.getUpperBounds();
            LogFactory.getLog().info("WildcardType.UpperBounds:");
            showType(lowerTypes[0]);
        }
    }

    public void startTest() throws Exception {
        LogFactory.getLog().info("");
        LogFactory.getLog().info("show parameterizedType( List<Integer> integerList):");
        showType(TypeModel.class.getDeclaredField("integerList").getGenericType());
        LogFactory.getLog().info("");
        LogFactory.getLog().info("show class ( String name):");
        showType(TypeModel.class.getDeclaredField("name").getGenericType());
        LogFactory.getLog().info("");

        LogFactory.getLog().info("show GenericArrayType ( T [] array):");
        showType(TypeModel.class.getDeclaredField("array").getGenericType());
        LogFactory.getLog().info("");

        LogFactory.getLog().info("show TypeVariable ( T data):");
        showType(TypeModel.class.getDeclaredField("data").getGenericType());
        LogFactory.getLog().info("");

        LogFactory.getLog().info("show primitive ( int number):");
        showType(TypeModel.class.getDeclaredField("number").getGenericType());
        LogFactory.getLog().info("");

        LogFactory.getLog().info("show WildcardType (  List<? extends Map> mapList):");
        showType(TypeModel.class.getDeclaredField("mapList").getGenericType());
        LogFactory.getLog().info("");

    }

    public static void main(String[] args) {
        try {
            new TestType().startTest();
        } catch (Exception e) {
            LogFactory.getLog().error(e);
        }
    }
}
